package com.xjnt.mnui.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.xjnt.frameworks.annotation.AutoInject;
import com.xjnt.frameworks.annotation.Router;
import com.xjnt.frameworks.core.CRUDController;
import com.xjnt.frameworks.exception.BusinessException;
import com.xjnt.frameworks.web.message.RetDataMsg;
import com.xjnt.frameworks.web.message.RetMsg;
import com.xjnt.mnui.entity.SubjectCount;
import com.xjnt.mnui.service.SubjectCountService;
import com.xjnt.mnui.service.SubjectService;

/** 
* 类说明：专题统计controller
* @author 作者 LUXIN
* @version 创建时间：2015年12月2日 上午11:23:33 
*/
@Router(name="/mnui/sub/count",view="/mnui/subjectCount")
public class SubjectCountController extends CRUDController<SubjectCount, SubjectCountService>{
	
	@AutoInject
	private SubjectCountService subjectCountService;
	
	@AutoInject
	private SubjectService subjectService;
	@Override
	protected String getBizName() {
		return "专题统计";
	}

	@Override
	protected Class<SubjectCount> getClazz() {
		return SubjectCount.class;
	}

	@Override
	protected SubjectCountService getService() {
		return subjectCountService;
	}
	/**
	 * 获取专题统计数据
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getCountAll(){
		Map<String, Object> map = new HashMap<String, Object>();
		// 分页信息
		int pageNumber = getParaToInt("pageNumber", 1);
		int pageSize = getParaToInt("pageSize", 10);
		
		int searchType = getParaToInt("searchType", 1);// 搜索类型 1：精确;0：模糊

		map.put("pageNumber", pageNumber);
		map.put("pageSize", pageSize);
		map.put("queryType", searchType);
		
		//页面查询条件
		map.put("subName", getPara("sub_name"));//查询条件:专题名称
		map.put("dataBegin", getPara("dataBegin"));//查询条件:统计期限：begin
		map.put("dataEnd", getPara("dataEnd"));//查询条件:统计期限：end
		
		List<Map> list = getService().getCountBySubject(map);//获得统计数据
		
		Map<String, Page<SubjectCount>> m1 = list.get(0);
		Map<String, Integer> m2 = list.get(1);
		
		Page<SubjectCount> page = m1.get("page");
		int total = m2.get("total");
		
		RetDataMsg dataMsg = new RetDataMsg(true);
		dataMsg.setTotal(total);
		dataMsg.setRows(JSON.parseArray(JsonKit.toJson(page.getList())));
		renderJson(dataMsg);
	}
	/**
	 * 打开根据时间段进行删除的页面
	 */
	public void goDel(){
		render("del.html");
	}
	/**
	 * 根据某时间段，批量删除统计日志
	 */
	public void deletes(){
		RetMsg retMsg = new RetMsg();
		try {
			getService().delsByDate(getPara("delDataBegin"), getPara("delDataEnd"));
			retMsg.pushOk("删除成功");
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("删除" + getBizName(), "发生异常：", e);
		}
	}
}
 